package com.sz.app.oss.api.logistics.base;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.base.service.CarrierCompanyService;
import com.sz.biz.logistics.base.dto.CarrierCompanyDto;
import com.sz.common.base.dto.I18nDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.service.I18nBaseService;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function: CarrierCompanyController <br>
 * Author: penghui.zheng <br>
 * Date: 2016-12-21 10:19:00
 */
@RestController
@RequestMapping("${api}/biz/carrierCompany")
@Api(description = " ", tags = "028、航空公司管理")
public class CarrierCompanyController extends BaseController {

    @Autowired
    private CarrierCompanyService carrierCompanyService;
    @Autowired
    private I18nBaseService i18nService;
    @Value("${i18n.biz.carrierCompany}")
    private String i18nTable;

    /**
     * 新建一个航空公司
     *
     * @throws Exception
     */
    @ApiOperation(value = "新建航空公司", notes = "新建航空公司 <br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    @ApiImplicitParams({@ApiImplicitParam(name = "carrierCompanyDto", value = "提交航空公司信息\r\n "
            + "name,figureCode,triadCode", required = true, dataType = "CarrierCompanyDto", paramType = "body")})
    @RequestMapping(value = "", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    public ResultDto saveCarrierCompany(@RequestBody CarrierCompanyDto carrierCompanyDto) {
        int id = carrierCompanyService.saveCarrierCompany(carrierCompanyDto);
        i18nService.save(id, carrierCompanyDto.getI18n(), i18nTable);
        ResultDto result = this.getSaveSuccessDto(MessagesCodes.MODULE_CARRIER_COMPANY);
        result.setData(id);
        return result;
    }

    /**
     * 修改航空公司
     *
     * @throws Exception
     */
    @ApiOperation(value = "修改航空公司", notes = "修改航空公司<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改航空公司的id ", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "CarrierCompanyDto", value = "提交航空公司信息\r\n "
                    + "name,figureCode,triadCode", required = true, dataType = "CarrierCompanyDto", paramType = "body")})
    @RequestMapping(value = "/{id}", method = {RequestMethod.PUT})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    public ResultDto updateCarrierCompany(@PathVariable int id,
                                          @RequestBody CarrierCompanyDto carrierCompanyDto) {
        carrierCompanyDto.setId(id);
        int flag = carrierCompanyService.updateCarrierCompany(carrierCompanyDto);
        i18nService.save(id, carrierCompanyDto.getI18n(), i18nTable);
        ResultDto dto = this.getUpdateSuccessDto(MessagesCodes.MODULE_CARRIER_COMPANY);
        dto.setData(flag);
        return dto;
    }

    /**
     * 根据id删除航空公司 ,逻辑删除
     *
     * @throws Exception
     */
    @ApiOperation(value = "删除一个航空公司", notes = "非物理删除 打标志 不允许使用<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ids ", required = true, dataType = "List", paramType = "body")})
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    public ResultDto deleteCarrierCompany(@RequestBody List<Integer> ids) {
        carrierCompanyService.deleteByIds(ids);
        return getDeleteSuccessDto(MessagesCodes.MODULE_CARRIER_COMPANY);
    }

    /**
     * 分页查询航空公司
     *
     * @throws Exception
     */
    @ApiOperation(value = "搜索航空公司", notes = "搜索航空公司<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "countryId", value = "countryId", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/searchlist", method = {RequestMethod.GET})
    public ResultDto findCarrierCompanyList(@RequestParam(required = false) String countryId,
                                            @RequestParam(required = false) String q, @RequestParam int pageIndex, @RequestParam int pageSize,
                                            @RequestParam(required = false) String sort,
                                            @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        Map<String, Object> map = new HashMap<>();
        map.put("countryId", countryId);
        ResultDto dto = carrierCompanyService
                .findCarrierCompanyList(new QueryParams(pageSize, pageIndex, q, sort, asc).put(map));
        return dto;
    }

    /**
     * 查询航空公司-短接口
     *
     * @throws Exception
     */
    @ApiOperation(value = "搜索航空公司-短接口", notes = "搜索航空公司-短接口<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "countryId", value = "countryId", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = true, dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/shortlist", method = {RequestMethod.GET})
    public ResultDto findShortCarrierCompanyList(@RequestParam(required = false) String countryId,
                                                 @RequestParam(required = false) String q,
                                                 @RequestParam int pageIndex,
                                                 @RequestParam int pageSize) {
        q = StringUtils.replaceSqlPattern(q);
        Map<String, Object> map = new HashMap<>();
        map.put("countryId", countryId);
        return carrierCompanyService
                .findShortCarrierCompanyList(new QueryParams(pageSize, pageIndex, q, null, true).put(map));
    }

    /**
     * 根据id查询航空公司
     *
     * @throws Exception
     */
    @ApiOperation(value = "搜索航空公司", notes = "搜索航空公司<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "航空公司的id ", required = true, dataType = "int", paramType = "path")})
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public ResultDto getCarrierCompanyById(@PathVariable int id) {
        CarrierCompanyDto carrierCompanyDto = carrierCompanyService.findById(id);
        ResultDto dto = new ResultDto();
        dto.setData(carrierCompanyDto);
        return dto;
    }

    /**
     * 根据条件获取国际化数据集合
     *
     * @param language  语言信息
     * @param q         参数信息
     * @param pageIndex 哪页
     * @param pageSize  分页大小
     * @param sort      排序字段的名称
     * @param asc       true asc false desc
     * @return 查询的国际化数据集合
     * @throws Exception 异常信息
     */
    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "查询i18n", notes = "根据指定条件获取国际化数据集合<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language", value = "语言信息", dataType = "string", paramType = "path", required = true),
            @ApiImplicitParam(name = "unfilled", value = "未填写项", dataType = "Boolean", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数,max<2000", dataType = "int", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query")}

    )
    @RequestMapping(value = "i18n/list/{language}", method = RequestMethod.GET)
    public ResultDto findI18n(@PathVariable String language, @RequestParam(required = false) String q,
                              @RequestParam(required = true) Boolean unfilled,
                              @RequestParam(required = false, defaultValue = "1") int pageIndex,
                              @RequestParam(required = false, defaultValue = "10") int pageSize,
                              @RequestParam(required = false) String sort,
                              @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        Map<String, Object> pd = new HashMap<>();
        pd.put("unfilled", unfilled);
        ResultDto dto = carrierCompanyService.findI18nByParam(new QueryParams(pageSize, pageIndex, q, sort, asc, language).put(pd));
        return dto;
    }

    /**
     * 批量新增或修改国际化.
     *
     * @param dtos 要保存或修改的国际化实体集合
     * @return 处理成功与否的返回信息
     */
    @ApiOperation(value = "新增或修改i18n", notes = "批量新增或修改航空公司国际化<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    @RequestMapping(value = "i18n", method = {RequestMethod.POST})
    public ResultDto saveI18n(@RequestBody @ApiParam(value = "要保存的对象集合", required = true) List<I18nDto> dtos) {
        i18nService.save(dtos, i18nTable);
        return getSaveI18nSuccessDto();
    }

    /**
     * 异步校验航空公司名称
     */
    @ApiOperation(value = "异步校验航空公司名称", notes = "异步校验航空公司名称<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "航空公司名称", required = true, dataType = "string", paramType = "query")})
    @RequestMapping(value = "/checkname", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    public ResultDto carrierCompanyCheck(@RequestParam(required = true) String name) {
        carrierCompanyService.checkCarrierCompanyName(name);
        return new ResultDto();
    }

    /**
     * 异步校验航空公司名称二字码
     */
    @ApiOperation(value = "异步校验航空公司名称二字码", notes = "异步校验航空公司名称二字码<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "figureCode", value = "航空公司名称二字码", required = true, dataType = "string", paramType = "query")})
    @RequestMapping(value = "/checkfigurecode", method = {RequestMethod.GET})
    public ResultDto figureCodeCheck(@RequestParam(required = false, defaultValue = "0") int id, @RequestParam(required = true) String figureCode) {
        carrierCompanyService.checkFigureCode(figureCode, id);
        return new ResultDto();
    }

    /**
     * 异步校验航空公司三字码
     */
    @ApiOperation(value = "异步校验航空公司三字码", notes = "异步校验航空公司三字码<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_AIRLINE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "triadCode", value = "航空公司三字码", required = true, dataType = "string", paramType = "query")})
    @RequestMapping(value = "/checktriadcode", method = {RequestMethod.GET})
    public ResultDto triadCodeCheck(@RequestParam(required = true) String triadCode) {
        carrierCompanyService.checkTriadCode(triadCode);
        return new ResultDto();
    }
}
